 /**
 * Description: 部门业务实现类
 * Copyright:Copyright 2018 nfky.com. All rights reserved
 * @author:xk
 * @since:0.0.1
 * Create at:2018-01-11 下午 18:32:34
 *  
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-01-11   xk       0.0.1     Initial
 */
package com.metro.paperless.sys.service.impl;

import com.metro.core.dao.IMapper;
import com.metro.core.service.BaseServiceSupport;
import com.metro.core.service.ServiceException;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import com.metro.paperless.sys.mapper.IFunctionMapper;
import com.metro.utils.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.metro.paperless.sys.mapper.ISysDeparmentMapper;
import com.metro.paperless.sys.service.inf.ISysDeparmentService;
import com.metro.paperless.sys.beans.vo.SysDeparment;

import java.util.List;

 /**
 * 部门业务实现类<br>
 *
 * @author xk
 * @since [0.0.1]
 * @version [0.0.1, 2018-01-11]
 */
@Service("sysDeparmentService")
public class SysDeparmentService extends BaseServiceSupport<SysDeparment> implements ISysDeparmentService{
    //部门Mapper
    @Autowired
    private ISysDeparmentMapper sysDeparmentMapper;
     @Autowired
     private IFunctionMapper functionMapper;

    @Override
    public IMapper getMapper() {
        return sysDeparmentMapper;
    }

     @Override
     public IResult batchUpdate(List<SysDeparment> deparments) {
         try {
             if(CollectionUtil.isNotEmpty(deparments)){
                 for(SysDeparment deparment : deparments){
                     sysDeparmentMapper.updateNotEmpty(deparment);
                 }
                 functionMapper.buidTreeTableInnerCode("sys_deparment");
             }
             return ResultCode.SUCCESS;
         } catch (Exception e) {
             log.error("修改部门失败", e);
             throw new ServiceException(ResultCode.ERROR);
         }
     }

     @Override
    public IResult save(SysDeparment entity) {
        try {
            sysDeparmentMapper.save(entity);
            functionMapper.buidTreeTableInnerCode("sys_deparment");
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增部门失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult update(SysDeparment entity) {
        try {
            sysDeparmentMapper.update(entity);
            functionMapper.buidTreeTableInnerCode("sys_deparment");
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改部门失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult delete(Long id) {
        try {
            SysDeparment entity = sysDeparmentMapper.getById(id);
            sysDeparmentMapper.deleteThisAndChild(entity);
            functionMapper.buidTreeTableInnerCode("sys_deparment");
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult deleteByIds(String ids) {
        try {
            sysDeparmentMapper.deleteByIds(ids);
            functionMapper.buidTreeTableInnerCode("sys_deparment");
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }
}